<?php
/**
 * @package EDO
 * @subpackage UI
 *
 */
class EDO_UI
{

	protected $edo;
	
	protected $_renderers;
	
	


	
	
	public function __construct(EDOHandler $edo)
	{
		$this->edo = $edo;
		require_once(dirname(__FILE__)."/renderers/renderer_base.php");
	}

	
	
	
	public function render(EDO_object_base $rObject, $fieldName, $renderMode = 'view', $config = array())
	{
		static $validationValues;
		if(!$validationValues)
		{
			$lead = LEAD::application();
			$validationValues = $lead->session->subSession('validationValues');
		}
				
				
		$fObject = $rObject->getFieldObject($fieldName);
		
		$validationValue = $validationValues->get($fieldName);
		if($validationValue !== false)
			$fObject->setValue($validationValue, false);
				
        	
		
		if(!isset($this->_renderers[$fObject->getType()]))
		{
            $rendererClass = false;
            $refleClass = new ReflectionClass($fObject);
            do
            {
                $classEnd = explode('_', $refleClass->getName(), 2);
                
                $classEnd = array_pop($classEnd);
                                
                $className = "renderer_{$classEnd}";
                 
                
                if(is_readable(dirname(__FILE__). "/renderers/{$className}.php"))
                {
                	$rendererClass = $className;
                }
                if(!$rendererClass)
                    $refleClass = $refleClass->getParentClass();
                               
            } while($rendererClass === false);
		    
		    			
			require_once dirname(__FILE__). "/renderers/{$rendererClass}.php";
			$this->_renderers[$fObject->getType()] = new $rendererClass();
		}

		$renderer = $this->_renderers[$fObject->getType()];
        
		$renderFunc = $renderMode . 'Render';
		if(is_callable(array($renderer, $renderFunc)))
            return $renderer->$renderFunc($fObject);
        else 				
		  return $renderer->render($fObject, $renderMode, $config);
		
	}
	
	
	
	
}
?>